package cn.wildfire.chat.app.extendsion.classes;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.HashMap;

import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;

/**
 * Create by HEKE, 2019/8/25 17:58
 * TODO 设置群组组员VIP权限
 */
public class AndroidVersionUpdateRequest {

    private Context context;
    private OnAndroidVersionCheckListener onAndroidVersionCheckListener;

    private long begainTime;

    public AndroidVersionUpdateRequest(Context context, OnAndroidVersionCheckListener onAndroidVersionCheckListener) {
        this.context = context;
        this.onAndroidVersionCheckListener = onAndroidVersionCheckListener;
        req();
    }

    /**
     * 获取当前app version code
     */
    public long getAppVersionCode() {
        long appVersionCode = 0;
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("", e.getMessage());
        }
        return appVersionCode;
    }

    private void req() {
        try {
            begainTime = System.currentTimeMillis();
            HashMap<String, String> params = new HashMap<>();
            String versionCode = "" + getAppVersionCode();
            params.put("androidVersionCode", versionCode);
            params.put("packageName", "" + context.getApplicationContext().getPackageName());
            OkHttpRequest.getNewInstance(context).requestAsyn(ServerUrlClient.ANDROID_VERSION_UPDATE, OkHttpRequest.TYPE_POST_JSON, params, new ReqCallBack<String>() {
                @Override
                public void onReqSuccess(String result) {
                    try {
                        Log.e("### Take Time", "  code : " + AndroidVersionUpdateRequest.this.hashCode() + "  take " + (System.currentTimeMillis() - begainTime) + " ms !");
                        if (!TextUtils.isTextEmpty(result)) {
                            JSONObject jsonObject = new JSONObject(result);
                            String code = jsonObject.getString("code");
                            if (code.equals("0")) {
                                JSONObject ret = jsonObject.getJSONObject("result");
                                AndroidVersion androidVersion = new Gson().fromJson(ret.toString(), AndroidVersion.class);
                                if (onAndroidVersionCheckListener != null) {
                                    onAndroidVersionCheckListener.update(!TextUtils.isTextEmpty(androidVersion.url), androidVersion.url);
                                }
                            } else {
                                if (onAndroidVersionCheckListener != null) {
                                    onAndroidVersionCheckListener.update(false, null);
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onReqFailed(String errorMsg) {
                    if (onAndroidVersionCheckListener != null) {
                        onAndroidVersionCheckListener.update(false, null);
                    }
                }
            }).request();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * Create by HEKE, 2019/8/25 18:19
     * TODO 版本检测回调结果接口
     */
    public interface OnAndroidVersionCheckListener {
        void update(boolean updated, String url);
    }

}
